<?php
class Post extends Controller {
	function Post(){
		parent::Controller();	
	}
	function index(){
		redirect('/dashboard/');
	}
	function view ($post_id){
		$this->load->model('User');
		$this->load->model('UsersDetail');
		$this->load->model('UserMahasiswa');
		$this->load->model('Posts');
		if($this->session->userdata('isLogin')){
			$data['namaLoggedIn'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
		}
		if($myEcho = $this->Posts->getPost($post_id)){
			$data['nama_prof'] = json_decode($this->UserMahasiswa->getNama($myEcho['user_id']))->nama;
			$data['username'] = $this->User->getUsername($myEcho['user_id']);
			$detail = json_decode($this->UsersDetail->getDetail($myEcho['user_id']));
			$data ['avatar'] = $detail->avatar;
			if($data['avatar']==''){
				$data['avatar'] = 'default_avatar.jpg';
			}
			$myEchoMsg ="";
			$myEchoMsg = $myEchoMsg.$this->_formatEcho($myEcho['user_id'],$myEcho['message'],$myEcho['date'],$myEcho['comments'],$myEcho['post_id']);	
			$data['myEchoMsg'] = $myEchoMsg;
		}
		$data['myEcho']=$myEcho;
		$data ['pageTitle'] = 'Echos '.$data['nama_prof'];
		$this->load->view('postView',$data);
	}
	
	function _formatEcho($user_id,$message,$date,$commentNum=NULL,$post_id=NULL){
		
		$this->load->model('UsersDetail');
		$this->load->model('UserMahasiswa');
		$this->load->model('User');
		
		$detail = json_decode($this->UsersDetail->getDetail($user_id));
		$avatar = $detail->avatar;
		$username = $this->User->getUsername($user_id);
		$nama = json_decode($this->UserMahasiswa->getNama($user_id))->nama;
		
		//formatting
		$message = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" rel=\"nofollow\">\\2</a>", $message);
		$message = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" rel=\"nofollow\">\\2</a>", $message);
  		$message = preg_replace("/@(\w+)/", "<a href=\"".base_url()."profile/info/"."\\1\" rel=\"nofollow\">@\\1</a>", $message);
  		$message = preg_replace("/@%(\w+)/", "<a href=\"".base_url()."groups/info/"."\\1\" rel=\"nofollow\">@\\1</a>", $message);
  		$message = preg_replace("/#(\w+)/", '<a href="'.base_url().'search/tag/'.'\\1" rel=\"nofollow\">#\\1</a>', $message);
		if($avatar==''){
			$avatar = 'default_avatar.jpg';
		}

		return'
		<li><a href="'.base_url().'profile/info/'.$username.'"><img class="avatarecho" src="'.base_url().'sknimg/i-see-attr/avatarThumb/'.$avatar.'" width="50px" height="50px" alt="avatar" /></a>
		<div class="tweetTxt">
		<strong><a href="'.base_url().'profile/info/'.$username.'">'.$nama.'</a></strong> '. $message.'
		<div class="date">'.date("l, F d, Y h:i" ,$date).'</div>
		<div class="date"><a href="javascript:;" onCLick="getComment('.$post_id.');">Komentar(<b id="commentnum_'.$post_id.'">'.$comm